package com.example.constellation.partnerfrag;

import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.constellation.R;
import com.example.constellation.util.AssetsUtil;
import com.example.constellation.util.LoadDataAsyncTask;
import com.example.constellation.util.URLContent;
import com.google.gson.Gson;
import de.hdodenhof.circleimageview.CircleImageView;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/**
 * 星座配对详情页
 */
public class PartnerAnalyslsActivity extends AppCompatActivity implements LoadDataAsyncTask.onGetNetDataListener{
    TextView titleTv,manTv,womanTv,pdTv,vsTv,gradeTv,contentTv;
    CircleImageView manIv,womanIv;
    ImageView backIv;
    private String man_name;
    private String man_logoname;
    private String woman_name;
    private String woman_logoname;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_partner_analysls);
        initView();
        //接收传来的数据
        getMsg();
        //获取网络路径地址
        String parnterURL=URLContent.getParnterURL(man_name,woman_name);
        /**
         * 加载网络数据
         */
        //创建自定义异步任务对象
        LoadDataAsyncTask task=new LoadDataAsyncTask(this,this,true);
        //执行异步任务
        task.execute(parnterURL);
    }
    //得到信息后执行此方法
    @Override
    public void onSucess(String json) {
        //解析数据
        if(!TextUtils.isEmpty(json)){
            PartnerAnalyslsBean pabean=new Gson().fromJson(json,PartnerAnalyslsBean.class);
            List<PartnerAnalyslsBean.NewList> newList=pabean.getNewslist();
            gradeTv.setText(newList.get(0).getGrade());
            contentTv.setText("解析:"+newList.get(0).getContent());
        }
    }
    private void  getMsg(){
        Intent intent=getIntent();
        man_name=intent.getStringExtra("man_name");
        man_logoname=intent.getStringExtra("man_logoname");
        woman_name=intent.getStringExtra("woman_name");
        woman_logoname=intent.getStringExtra("woman_logoname");
        //设置直接显示的文本
        Map<String, Bitmap> map=AssetsUtil.getContentlogoImgMap();
        Bitmap manBitmap=map.get(man_logoname);
        manIv.setImageBitmap(manBitmap);
        Bitmap womanBitmap=map.get(woman_logoname);
        womanIv.setImageBitmap(womanBitmap);

        manTv.setText(man_name);
        womanTv.setText(woman_name);
        pdTv.setText("星座配对--"+man_name+"和"+woman_name+"配对");
        vsTv.setText(man_name+"vs"+woman_name);
    }
    private void initView(){
        manIv=findViewById(R.id.parnteranalysis_iv_man);
        womanIv=findViewById(R.id.parnteranalysis_iv_woman);
        backIv=findViewById(R.id.title_iv_back);
        manTv=findViewById(R.id.parnteranalysis_tv_man);
        womanTv=findViewById(R.id.parnteranalysis_tv_woman);
        pdTv=findViewById(R.id.parnteranalysis_tv_pd);
        vsTv=findViewById(R.id.parnteranalysis_tv_vs);
        gradeTv=findViewById(R.id.parnteranalysis_tv_grade);
        contentTv=findViewById(R.id.parnteranalysis_tv_content);
        titleTv=findViewById(R.id.title_tv);
        backIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();

            }
        });
    }


}
